package Messages;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import Server.SQLConnection;
import ocsf.server.ConnectionToClient;


public class MessageStopScenario {

	private ConnectionToClient client ;
	private ArrayList<Object> str ;
	private int per,run, all;
	
public MessageStopScenario(ArrayList<Object> msg, ConnectionToClient client) {
		
		this.client= client ;
		this.str= msg; 		
		
	}

public void Run(String ScenarioName) throws SQLException, IOException {
	SQLConnection sc = new SQLConnection();
	
	PreparedStatement Allps = sc.getConnection().prepareStatement("SELECT count(*) FROM hts."+ScenarioName+";");
	PreparedStatement Runps = sc.getConnection().prepareStatement("SELECT count(*) FROM hts."+ScenarioName+" where Status='NoRun' ");
	
	ResultSet Allrs= Allps.executeQuery();
	ResultSet Runrs= Runps.executeQuery();
	
	while(Allrs.next())
		all = Allrs.getInt(1);
	while(Runrs.next())	
		run = Runrs.getInt(1);
	
	per = 100- ((run *100)/all) ;
	
	if ( per == 100 )
	{
		java.sql.PreparedStatement ScenarioTemp = sc.getConnection().prepareStatement("UPDATE hts.run SET Status= ? , per = ? where SName='"+ScenarioName+"';");
		ScenarioTemp.setString(1, "Finished");
		ScenarioTemp.setInt(2,per);
		ScenarioTemp.executeUpdate();
		ScenarioTemp.close();

	}
	else
	{
		java.sql.PreparedStatement ScenarioTemp = sc.getConnection().prepareStatement("UPDATE hts.run SET Status= ? , per = ? where SName='"+ScenarioName+"';");
		ScenarioTemp.setString(1, "Stop");
		ScenarioTemp.setInt(2,per);
		ScenarioTemp.executeUpdate();
		ScenarioTemp.close();

	}
	
	
	
	
	

	
	PreparedStatement Centerps = sc.getConnection().prepareStatement("SELECT * FROM Run;");	
	
	ResultSet Centerrs= Centerps.executeQuery();	        			
	ArrayList<Object> Center =new ArrayList<Object>();
	Center.add("Manager");
	
	while (Centerrs.next())
	{
		Center.add(Centerrs.getInt(1));
		Center.add(Centerrs.getString(2));
		Center.add(Centerrs.getString(3));
		Center.add(Centerrs.getInt(4));
		Center.add(Centerrs.getString(5));

	}
	
	
	PreparedStatement CCRps = sc.getConnection().prepareStatement("SELECT SName FROM hts.project where Project='CCR';");	
	
	ResultSet CCRrs= CCRps.executeQuery();	        			
	
	Center.add("CCR");
	
	while (CCRrs.next())
	{			
		Center.add(CCRrs.getString(1));
	
	}
	
	PreparedStatement VRSps = sc.getConnection().prepareStatement("SELECT SName FROM hts.project where Project='VRS';");	
	
	ResultSet VRSrs= VRSps.executeQuery();	        			
	
	Center.add("VRS");
	
	while (VRSrs.next())
	{			
		Center.add(VRSrs.getString(1));
	
	}
	
	PreparedStatement Otherps = sc.getConnection().prepareStatement("SELECT SName FROM hts.project where Project='Other';");	
	
	ResultSet Otherrs= Otherps.executeQuery();	        			
	
	Center.add("Other");
	
	while (Otherrs.next())
	{			
		Center.add(Otherrs.getString(1));
	
	}
	
	client.sendToClient(Center);
	
	Runps.close();
	Runrs.close();
	Allrs.close();
	Centerps.close();
	Allps.close();
	Centerrs.close();
	
	
	
	
	
	
}

}
